package org.jeecg.modules.flow.controller;

import cn.hutool.core.util.XmlUtil;
import org.jeecg.modules.flow.util.PositionTransducer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created on 2024/3/25.
 *
 * @author hua
 */
public class WayLineAnalysisMain {

    public static void main(String[] args) throws Exception {
        List<String> nameList = Arrays.asList("靶场");
        nameList.forEach(v -> {
            printLine(v);
        });
    }

    private static void printLine(String name) {
        try {
            File file = new File("C:\\Users\\admin\\Desktop\\巡检一张图\\航线\\新的\\" + name + "\\wpmz\\waylines.wpml");
        /*BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        StringBuffer sbResource = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sbResource.append(line);
            //sbResource.append("\r\n");
        }
        //关流
        System.out.println(sbResource);*/

            //输入流
            FileInputStream fis = new FileInputStream(file);
            //字节为 文件的长度
            byte[] data = new byte[(int) file.length()];
            //读取
            fis.read(data);
            //关流
            fis.close();
            //创建String文件
            String xmlContent = new String(data, StandardCharsets.UTF_8);
            //System.out.println(xmlContent);
            //获取文档
            Document document = XmlUtil.parseXml(xmlContent);
            //获取根节点
            Element elementG = XmlUtil.getRootElement(document);

            List<String> split = getElementValueByName(elementG, "coordinates");

            StringBuffer sb = new StringBuffer();
            for (String arg : split) {
                arg = arg.trim();
                String[] st = arg.split(",");

                double[] doubles = PositionTransducer.wgs2gcj(new Double(st[1]), new Double(st[0]));

                sb.append(doubles[1] + "," + doubles[0] + ";");
            }
            System.out.println(name);
            System.out.println(sb.substring(0, sb.length() - 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String> getElementValueByName(Element element, String name) {
        //从节点里，获取这个 子节点
        NodeList nodeList = element.getElementsByTagName(name);
        List<String> stringList = new ArrayList<>();
        //遍历 放入lists
        for (int i = 0; i < nodeList.getLength(); i++) {
            stringList.add(nodeList.item(i).getTextContent());
        }
        return stringList;
    }

    //方式1：使用根节点下的直接 转成对象
    //DepartCarResult departCarResult = XmlUtil.xmlToBean(elementG, DepartCarResult.class);
    //方式2：获取 根节点下 cars 节点
    //Element carsElement = XmlUtil.getElement(elementG, "coordinates");
    //cars 节点是个 List，每一个对象为 car
    //CarListModel carListModel = XmlUtil.xmlToBean(carsElement, CarListModel.class);

    public static String str = "";


}
